<!--
  作者：xhr
  时间：2022年08月19日 16:40:10
-->
<template>
    <div style="padding: 10px">
        <div style="margin: 10px 0">
            <el-button style="margin-right: 10px;" type="primary" @click="add">新增</el-button>
            <el-popconfirm style="margin-right: 10px;" class="ml-5" confirm-button-text='好的' cancel-button-text='不用了'
                icon="el-icon-info" icon-color="red" title="您确定删除吗？" @confirm="delBath">
                <el-button plain class="el-icon-circle-close" type="danger" slot="reference">批量删除</el-button>
            </el-popconfirm>
            <el-input v-model="search" placeholder="搜索文章标题" style="width: 20%" clearable @clear="reply"></el-input>
            <el-button type="primary" style="margin-left: 10px" @click="loading">查询</el-button>
        </div>
        <el-table :data="tableData" border stripe max-height="700">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column sortable prop="id" label="ID"></el-table-column>
            <el-table-column prop="title" label="标题"></el-table-column>
            <el-table-column prop="author" label="作者"></el-table-column>
            <el-table-column prop="time" label="创建时间"></el-table-column>
            <el-table-column label="轮播图设置">
                <template slot-scope="scope">
                    <!-- <el-button @click="viewPaper(scope.row.id)" size="mini">查看</el-button> -->
                    <el-switch v-model="scope.row.isHot" @change="chooseSwiper(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-button @click="details(scope.row)" size="mini">详情</el-button>
                    <el-button @click="update(scope.row)" size="mini">编辑</el-button>
                    <el-popconfirm title="确定删除吗？" style="margin-left: 5px" @confirm="delate(scope.row.id)">
                        <el-button type="danger" size="mini" slot="reference">删除
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                :page-sizes="[5, 10, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
            <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
                <el-form label-width="120px" :model="form">
                    <el-form-item label="标题">
                        <el-input v-model="form.title" style="width: 80%"></el-input>
                    </el-form-item>
                <div id="div1">

                    </div>
                    <!-- <el-form-item label="内容">
                            <el-input v-model="form.content" style="width: 80%"></el-input>
                        </el-form-item> -->
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="save">确 定</el-button>
                </span>
            </el-dialog>
            <el-dialog title="详情" :visible.sync="vis" width="50%">
                <el-card>
                    <div v-html="detail.content" style="min-height:100px"></div>
                </el-card>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import request from "@/utils/request";
import E from 'wangeditor';

let editor;
export default {
    name: "News",
    data() {
        return {
            vis: false,
            form: {

            },
            // isHot:false,
            total: 0,
            currentPage: 1,
            pageSize: 10,
            search: "",
            dialogVisible: false,
            tableData: [],
            detail: {},
        };
    },
    created() {
        this.loading();
    },
    mounted() {

    },
    methods: {
        filesUploadSuccess(res) {
            this.form.cover = res.data

        },
        loading() {
            request
                .get("/news/page", {
                    params: {
                        pagenum: this.currentPage,
                        pagesize: this.pageSize,
                        search: this.search,
                    },
                })
                .then((res) => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                });
        },
        save() {
            this.form.content = editor.txt.html()


            if (this.form.id) {
                request.put("/news", this.form).then((res) => {
                    console.log(res);
                    if (res.code === "0") {
                        this.$message({
                            type: "success",
                            message: "更新成功",
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: "更新失败",
                        });
                    }
                    this.loading();
                    this.dialogVisible = false;
                    editor.txt.html("")
                });
            } else {
                let userStr = sessionStorage.getItem("user") || "{}"
                let user = JSON.parse(userStr)
                this.form.author = user.nickName
                request.post("/news", this.form).then((res) => {
                    console.log(res);
                    if (res.code === "0") {
                        this.$message({
                            type: "success",
                            message: "新增成功",
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: "新增失败",
                        });
                    }
                    this.loading();
                    this.dialogVisible = false;
                    editor.txt.html("")
                });
            }
        },
        chooseSwiper(row) {
            this.form = row;
            // this.form.isHot=!this.form.isHot;
            // console.log(this.form.isHot);
            request.put("/news/changeHot", this.form).then((res) => {
                console.log(res);
                if (res.code === "0") {
                    this.$message({
                        type: "success",
                        message: "轮播图更新成功",
                    });
                } else {
                    row.isHot = !row.isHot;
                    this.$message({
                        type: "error",
                        message: res.msg,
                    });
                }
            });
        },
        add() {
            this.dialogVisible = true;
            this.form = {};


            this.$nextTick(() => {
                if (!editor) {
                    editor = new E("#div1");
                    editor.config.uploadImgServer = 'http://localhost:9090/files/edit/upload'
                    editor.config.uploadFileName = "file"
                    editor.create()
                }
                editor.txt.html("")
            })


        },
        update(row) {
            this.form = JSON.parse(JSON.stringify(row));
            this.dialogVisible = true;

            this.$nextTick(() => {
                if (!editor) {
                    editor = new E("#div1");
                    editor.config.uploadImgServer = 'http://localhost:9090/files/edit/upload'
                    editor.config.uploadFileName = "file"
                    editor.create()
                }
                editor.txt.html(this.form.content)
            })



        },
        details(row) {

            this.detail = row
            this.vis = true

        },
        delate(id) {
            request.delete("/news/" + id).then((res) => {
                if (res.code === "0") {
                    this.$message({
                        type: "success",
                        message: "删除成功",
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: "删除失败",
                    });
                }
            });
            this.loading();
        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.loading();
        },
        handleCurrentChange(pageNum) {
            this.currentPage = pageNum;
            this.loading();
        },
        reply() {
            this.loading();
        },
        delBath() {
            // if (!this.ids.length) {
            //     this.$message.warning("请选择数据！")
            //     return
            // }
            // request.delete("/activity/delBatch/" + this.ids).then(res => {
            //     this.$message.success("批量删除成功！");
            //     this.loading()
            // })
        },
    },
};
</script>

<style></style>